package com.zephyr.vchcenter.dwrAction;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.zephyr.vchcenter.bean.BaseObject;
import com.zephyr.vchcenter.bean.ConfigData;
import com.zephyr.vchcenter.bean.ConfigKey;
import com.zephyr.vchcenter.bean.PbcOriginalData;
import com.zephyr.vchcenter.bean.Voucher;
import com.zephyr.vchcenter.dao.IBaseDao;
import com.zephyr.vchcenter.helper.SpringHelper;
import com.zephyr.vchcenter.pbcFile.exception.ConfigParseException;
import com.zephyr.vchcenter.pbcFile.exception.MatchException;
import com.zephyr.vchcenter.pbcFile.exception.RepeatException;
import com.zephyr.vchcenter.pbcFile.service.IPbcFileParseService;
import com.zephyr.vchcenter.pbcFile.service.impl.PbcFileParseServiceImpl;
import com.zephyr.vchcenter.pbcFile.util.FileParserUtil;
import com.zephyr.vchcenter.pbcFile.util.UploadFileUtil;
public class UploadPbcFileDwrAction {
  private static final Logger log = Logger.getLogger(UploadPbcFileDwrAction.class);
  public Map<String,String> operate(String file,String subno,String fileName){
	  log.debug(file+"=====>====>");
	  FileParserUtil fileParserUtil=new FileParserUtil();
	  //log.debug(file.substring(0,1));
	  byte[] fileContentTemp=null;
	  byte[] fileContent=null;
	try{
	  String temp=fileParserUtil.getDecoder(file);
	  //log.debug(file);
	  fileContentTemp =temp.getBytes();
	  //log.debug(fileContentTemp.length+"=====>>>>======>>=");
	  String strTemp=new String(fileContentTemp);
	  //log.debug(strTemp);
	  fileContent=strTemp.getBytes("utf-8");
	  //String ss=new String(fileContent,"utf-8");
	  //log.debug(ss);
	  //log.debug(fileContent.length+"============");
	  //log.debug(fileContent);
     } catch (UnsupportedEncodingException e1) {
		log.error(e1.getMessage());
     } 
      String date=null;//日期
	  String roundno=null;//场次
	  String errorMessage="";//错误信息
	  IBaseDao dao=(IBaseDao)SpringHelper.getBean("baseDao");
	  ConfigData configDataTemp=new ConfigData();
	  configDataTemp.setSubno(subno);
	  List<ConfigData> list=dao.findByExample(configDataTemp);
	  for(ConfigData configData:list){
		  if(ConfigKey.VCHZMS_PBC_DATE.equals(configData.getKey())){
			  date=configData.getValue();
		  }
		  if(ConfigKey.VCHZMS_PBC_SESSION.equals(configData.getKey())){
			  roundno=configData.getValue();  
		  } 
	  }
	//服务器端生成临时文件
	  File tempfile=null;
		try {
			tempfile =File.createTempFile(fileName, null);
			UploadFileUtil.storeFile(fileContent, tempfile);
			log.debug(tempfile.length()+"---------");
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		//调用人行文件解析服务 解析ӿ� ���ļ����н���
		IPbcFileParseService pbcFileParseService = new PbcFileParseServiceImpl();
		PbcOriginalData pbcOriginalData = new PbcOriginalData();
		pbcOriginalData.setSubno(subno);
		pbcOriginalData.setVchdate(date);
		pbcOriginalData.setVcsession(roundno);
		Map<String,Object> map = null;
		Map<String,String> returnValue=new HashMap<String, String>();
		try {
			log.debug("begin parser............");
			map = pbcFileParseService.parseFile(tempfile, subno,
					pbcOriginalData);
			List<BaseObject> successfulList=(List<BaseObject>)map.get("successfulData");
			List<Object> failList=(List<Object>)map.get("failData");
			Integer totalSize=(Integer)map.get("totalNum");
			returnValue.put("successful", successfulList.size()+"");
			returnValue.put("fail",failList.size()+"");
			returnValue.put("total",totalSize+"");
			log.debug("parse is ok......");
			pbcFileParseService.persistSuccessful(successfulList);
			log.debug("persist is ok...........");
	  //File file = File.createTempFile(fileName, null);
		}catch (MatchException e) {
			//request.setAttribute("error", "");
		    //e.printStackTrace();
			errorMessage+="无匹配信息";
		} catch (ConfigParseException e) {
			//request.setAttribute("error", "���кŴ���");
			//e.printStackTrace();
			errorMessage+="配置信息加载失败";
		} catch (RepeatException e) {
			//request.setAttribute("error", e.getMessage());
			//e.printStackTrace();
			errorMessage+=e.getMessage();
		} catch(Exception e){
			//e.printStackTrace();
		}
		returnValue.put("errorMessage", errorMessage);
        return returnValue;
}
}
